package com.antd.admin.server.controller;


import com.antd.admin.api.entity.SysDept;
import com.antd.admin.server.service.ISysDeptService;
import com.antd.common.core.util.R;
import com.antd.common.log.annotation.SystemLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author 子丶沫
 * @since 2020-11-18
 */
@RestController
@RequestMapping("/dept")
@Api("部门管理")
public class SysDeptController {
    @Autowired
    private ISysDeptService deptService;
    @SystemLog(description = "查询部门树")
    @ApiOperation("查询部门树")
    @GetMapping("/getDeptTree")
    public R<Object> getDeptTree(){
        return R.ok(deptService.getDeptTree());
    }

    @GetMapping("searchDept")
    public R<Object> searchDept(String searchText){
        return R.ok(deptService.searchDept(searchText));
    }

    @PostMapping
    @ApiOperation("保存部门")
    @SystemLog(description = "保存部门")
    @PreAuthorize("hasAnyAuthority('sys:dept:save')")
    public R<Object> saveDept(@RequestBody SysDept dept){
        return R.ok(deptService.save(dept));
    }
    @PutMapping
    @ApiOperation("修改部门")
    @SystemLog(description = "修改部门")
    @PreAuthorize("hasAnyAuthority('sys:dept:update')")
    public R<Object> updateDept(@RequestBody SysDept dept){
        return R.ok(deptService.updateById(dept));
    }

    @DeleteMapping("/{deptId}")
    @ApiOperation("删除部门")
    @SystemLog(description = "删除部门")
    @PreAuthorize("hasAnyAuthority('sys:dept:delete')")
    public R<Object> updateDept(@PathVariable String deptId){
        return R.ok(deptService.deleteDept(Collections.singletonList(deptId)));
    }

    @DeleteMapping("/batchDelete")
    @ApiOperation("批量删除部门")
    @SystemLog(description = "批量删除部门")
    @PreAuthorize("hasAnyAuthority('sys:dept:delete')")
    public R<Object> batchDelete(@RequestParam("ids") String ids){
        List<String> idsList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        return R.ok(deptService.deleteDept(idsList));
    }
}
